package com.asc.service.user.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.asc.service.user.entity.RoleEXT;
import com.asc.service.user.entity.UserEXT;
import com.asc.service.user.service.RoleService;
import com.asc.service.user.service.UserService;
import com.asc.utils.HTMLUtil;
import com.asc.utils.ObjectUtil;
import com.asc.utils.Page;
import com.asc.utils.ResultWrap;

//角色管理
@RestController
@RequestMapping("/role")
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/queryRole")
	public void queryRole(HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> requestMap = HTMLUtil.getPageParams(request);
		System.out.println(requestMap);
		ResultWrap resultWrap = HTMLUtil.getDefaultWrap();
		List<RoleEXT> roles = roleService.queryRole(requestMap);
		resultWrap.setData(roles);
		//分页信息
		if(ObjectUtil.isNotBlank(requestMap.get("iDisplayLength"))) {
			requestMap.put("cnt", true);
			int resultCNT = roleService.queryRoleCount(requestMap);
			resultWrap.setiTotalDisplayRecords(resultCNT);
			resultWrap.setiTotalRecords(resultCNT);
		}
		HTMLUtil.sendWrap(response, resultWrap, request);
	}
	
	@RequestMapping("/saveRole")
	public void saveRole(HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> requestMap = HTMLUtil.getPageParams(request);
		System.out.println(requestMap);
		ResultWrap resultWrap = HTMLUtil.getDefaultWrap();
		if(ObjectUtil.isNotBlank(requestMap.get("name")) && ObjectUtil.isNotBlank(requestMap.get("checkboxSelVal"))) {
			Map<String,Object> checkNameMap = new HashMap<String,Object>();
			checkNameMap.put("name", requestMap.get("name"));
			checkNameMap.put("noid", requestMap.get("id"));
			checkNameMap.put("delstate", 1);
			List<RoleEXT> checkRoles = roleService.queryRole(checkNameMap);
			if(checkRoles.size() >= 1) {
				resultWrap.setFlag(0);
				resultWrap.setMessage("角色名称重复");
				HTMLUtil.sendWrap(response, resultWrap, request);
				return;
			}
			
			int saveResult = 0;
			if(ObjectUtil.isNotBlank(requestMap.get("id"))) {//id不为空则修改
				saveResult = roleService.updateRole(requestMap);
			}else {//id为空，添加
				saveResult = roleService.saveRole(requestMap);
			}
			if(saveResult >= 1) {
				if(ObjectUtil.isBlank(requestMap.get("id"))) {
					resultWrap.setFlag(0);
					resultWrap.setMessage("权限保存失败");
					HTMLUtil.sendWrap(response, resultWrap, request);
					return;
				}
				//通过id删除之前有的权限
				roleService.deleteRoleNavigate(requestMap);
				//通过id添加新的权限
				requestMap.put("navs", requestMap.get("checkboxSelVal").toString().split(","));
				int resultSaveNewNavigate = roleService.saveRoleNavigate(requestMap);
				if(resultSaveNewNavigate >= 1) {
					resultWrap.setSaveSuccess();
				}else {
					resultWrap.setSaveFailure();
				}
			}else {
				resultWrap.setSaveFailure();
			}
		}else {
			resultWrap.setParamIsnotEmpty("角色名称和权限都不能为空！");
		}
		HTMLUtil.sendWrap(response, resultWrap, request);
	}
	
	/**
	 * 删除角色
	 * */
	@RequestMapping("/deleteRole")
	public void deleteRole(HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> requestMap = HTMLUtil.getPageParams(request);
		System.out.println(requestMap);
		ResultWrap resultWrap = HTMLUtil.getDefaultWrap();
		if(ObjectUtil.isNotBlank(requestMap.get("id"))) {
			//检查该角色下是否存在用户
			Map<String,Object> userMap = new HashMap<String,Object>();
			userMap.put("roleid", requestMap.get("id"));
			userMap.put("delstate", 1);
			userMap.put("type", "admin");
			List<UserEXT> users = userService.queryUser(userMap);
			if(users.size() >= 1) {
				resultWrap.setFlag(0);
				resultWrap.setMessage("该角色存在用户，不能删除");
				HTMLUtil.sendWrap(response, resultWrap, request);
				return;
			}
			int delResult = roleService.deleteRole(requestMap);
			if(delResult >= 1) {
				roleService.deleteRoleNavigate(requestMap);
				resultWrap.setDeleteSuccess();
			}else {
				resultWrap.setDeleteFailure();
			}
		}else {
			resultWrap.setParamIsnotEmpty("角色编号不能为空");
		}
		HTMLUtil.sendWrap(response, resultWrap, request);
	}
}
